const { imgUrl } = getApp().globalData;
import { mineApi } from "../../../utils/request/api/mine";
import { publicApi } from "@/utils/request/api/public";
import Toast from "@vant/weapp/toast/toast";
import Dialog from "@vant/weapp/dialog/dialog";

Page({
  data: {
    id: 0,
    imgUrl: imgUrl + "mine/",
    show: false,
    newName: "",
    list: [],
  },

  changeNewName(event) {
    this.setData({
      newName: event.detail,
    });
  },
  getUserInfo(event) {
    let obj = {
      name: this.data.newName, //收藏夹名称
      note: this.data.newName, //收藏夹备注,
    }
    let title = '添加成功';

    if (this.data.id != 0) {
      obj.id = this.data.id
      title = '修改成功';
    }
    mineApi
      .addFloder(obj)
      .then((res) => {
        console.log(res);
        wx.showToast({
          title,
          icon: "success",
          duration: 2000,
          success: () => {
            this.getList();
          },
        });
      });
  },

  onClose() {
    setTimeout(() => {
      this.setData({ show: false });
    }, 1000);
  },
  showDialog() {
    this.setData({ show: true, id:0 });
  },
  getList() {
    mineApi.getUserCollectList().then((res) => {
      this.setData({
        list: res.data.map((item) => {
          item.updated_at = item.updated_at.split(" ")[0];
          return item;
        }),
      });
    });
  },
  editCollect(event) {
    const { id, name } = event.currentTarget.dataset;
    this.setData({
      show: true,
      newName: name,
      id
    });
  },
  deleteCollect(event) {
    Dialog.confirm({
      title: "提示",
      message: "确定要删掉这个文件夹吗？",
    })
      .then(() => {
        // on confirm
        const { id } = event.currentTarget.dataset;
        mineApi.delFloder({ id }).then((res) => {
          console.log(res);
          wx.showToast({
            title: "删除成功",
            icon: "success",
            duration: 2000,
            success: () => {
              this.getList();
            },
          });
        });
      })
      .catch(() => {
        // on cancel
      });
  },
  toDetail(event) {
    const { id } = event.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/mine/collect/collect?id=${id}`,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
